<template>
    <app-layout>
        <view class="info">
            <view class="last">可提现金额：￥{{region.total_bonus ? region.total_bonus : '0.00'}}</view>
        </view>
        <view>
            <view class="info input-money main-between cross-center">
                <view class="input">
                    <input v-model="moneyInput" type="digit" placeholder-style="color:#cdcdcd" placeholder="输入提现金额">
                    <view class="price">￥</view>
                </view>
            </view>
            <view class="about"
                  v-if="config.min_money > 0 || config.cash_service_charge > 0 || config.free_cash_min > 0 || config.free_cash_max > 0">
                <view class="dir-left-nowrap">
                    <view>说明：</view>
                    <view>
                        <view v-if="config.min_money > 0">金额不能少于￥{{config.min_money}}</view>
                        <view v-if="config.cash_service_charge > 0">提现需要加收{{config.cash_service_charge}}%手续费</view>
                        <view v-if="config.free_cash_max > 0">
                            免手续费提现金额区间为￥{{config.free_cash_min}}~￥{{config.free_cash_max}}
                        </view>
                    </view>
                </view>
            </view>

            <view class="dir-left-nowrap cross-center cash-type" @click="cashTypeModel = true">
                <view class="box-grow-1">提现方式</view>
                <view class="box-grow-0">{{cashName}}</view>
                <image class="box-grow-0 arrow-image" src="/static/image/icon/arrow-right.png"></image>
            </view>

            <view v-if="mark_num==1 || mark_num ==2 || mark_num == 3" class="cash-info">
                <view class="info content">
                    <view class="enter" v-if="mark_num==3">开户人</view>
                    <view class="enter" v-else>姓名</view>
                    <input class="info-input" v-model="name" placeholder-style="color:#cdcdcd" placeholder="请输入正确的姓名"></input>
                </view>
                <view class="info content" v-if="mark_num==3">
                    <view class="enter">开户行</view>
                    <input class="info-input" v-model="bank_name" placeholder-style="color:#cdcdcd" placeholder="请输入正确的银行名称"></input>
                </view>
                <view class="info content">
                    <view class="enter">帐号</view>
                    <input class="info-input" v-model="mobile" v-if="mark_num==1" placeholder-style="color:#cdcdcd" placeholder="请输入正确的微信帐号"></input>
                    <input class="info-input" v-model="mobile" v-if="mark_num==2" placeholder-style="color:#cdcdcd" placeholder="请输入正确的支付宝帐号"></input>
                    <input class="info-input" v-model="mobile" v-if="mark_num==3" type="number"
                           placeholder-style="color:#cdcdcd" placeholder="请输入正确的银行卡帐号"></input>
                </view>
            </view>
            <view class="submit">
                <button @click="subscribe" class="red-background">提交申请</button>
            </view>
        </view>
        <view class="bg" v-if="submitOver || warningInfo">
            <view class="dialog">
                <view class="dialog-title">提示</view>
                <view v-if="submitOver">提现申请提交成功</view>
                <view v-if="warningInfo">{{warningInfo}}</view>
                <view @click="toDetail" v-if="submitOver" class="close-button">确认</view>
                <view @click="warningInfo = false" v-if="warningInfo" class="close-button">确认</view>
            </view>
        </view>
        <app-cash-model
                :is-auto="pay_type.auto"
                :is-wx="pay_type.wechat"
                :is-alipay="pay_type.alipay"
                :is-bank="pay_type.bank"
                :is-balance="pay_type.balance"
                :pay-type="cashType"
                @change="payTypeChange"
                v-model="cashTypeModel"
        ></app-cash-model>
    </app-layout>
</template>

<script>
    import {mapState} from "vuex";
    import appCashModel from "../../../components/page-component/app-cash-model/app-cash-model.vue"

    export default {
        data() {
            return {
                cashTypeModel: false,
                visible: false,
                mark_num: -1,
                region: {},
                moneyInput: '',
                pay_type: {
                    auto: false,
                    alipay: false,
                    wechat: false,
                    bank: false,
                    balance: false,
                },
                loading: false,
                warningInfo: '',
                submitOver: false,
                config: [],
                name: '',
                mobile: '',
                template_message: [],
                getMoney: null,
                cashType: '',
            }
        },
        components: {appCashModel,},
        computed: {
            ...mapState({
                mall: state => state.mallConfig.mall,
            }),
            cashName() {
                switch (this.cashType) {
                    case 'auto':
                        // #ifdef MP-WEIXIN
                        return '微信零钱';
                        // #endif
                        // #ifdef MP-ALIPAY
                        return '支付宝余额';
                        // #endif
                        // #ifndef MP-WEIXIN || MP-ALIPAY
                        return '自动';
                    // #endif
                    case 'wx':
                        return '微信线下打款';
                    case 'alipay':
                        return '支付宝线下打款';
                    case 'bank':
                        return '银联线下打款';
                    case 'balance':
                        return '商城余额';
                    default:
                        return;
                        break;
                }
            },
        },
        methods: {
            payTypeChange(value) {
                this.cashType = value;
                switch (value) {
                    case 'auto':
                        this.mark_num = 0;
                        break;
                    case 'wx':
                        this.mark_num = 1;
                        break;
                    case 'alipay':
                        this.mark_num = 2;
                        break;
                    case 'bank':
                        this.mark_num = 3;
                        break;
                    case 'balance':
                        this.mark_num = 4;
                        break;
                    default:
                        this.mark_num = -1;
                        break;
                }
            },
            subscribe() {
                if (this.loading) {
                    return false
                }
                if (this.submitOver || this.warningInfo) {
                    return false
                }
                if (this.moneyInput > 0) {
                    if (this.mark_num == -1) {
                        uni.showToast({title: '请选择提现方式', icon: 'none'});
                        return;
                    }
                    this.loading = true;
                    this.$subscribe(this.template_message).then(res => {
                        this.submit();
                    }).catch(res => {
                        this.submit();
                    });
                }else {
                    this.warningInfo = '请输入正确的提现金额'
                }
            },
            submit() {
                let that = this;
                uni.showLoading({
                    mask: true,
                    title: '加载中...'
                });
                let para = {
                    price: that.moneyInput,
                    type: 'auto'
                };
                if (that.mark_num == 4) {
                    para.type = 'balance';
                } else if (that.mark_num == 0) {
                    para.type = 'auto';
                } else {
                    para.name = that.name;
                    para.mobile = that.mobile;
                    if (that.mark_num == 1) {
                        para.type = 'wechat';
                    }
                    if (that.mark_num == 2) {
                        para.type = 'alipay';
                    }
                    if (that.mark_num == 3) {
                        para.type = 'bank';
                        para.bank_name = that.bank_name;
                    }
                }
                that.$request({
                    url: that.$api.region.cash,
                    data: para,
                    method: 'post'
                }).then(response=>{
                    uni.hideLoading();
                    that.loading = false;
                    if(response.code == 0) {
                        that.submitOver = true;
                    }else {
                        that.warningInfo = response.msg;
                    }
                }).catch(response => {
                    that.loading = false;
                    uni.hideLoading();
                });
            },

            toDetail() {
                this.submitOver = false;
                uni.redirectTo({
                    url: '/plugins/region/cash-detail/cash-detail'
                });
            },

            getStatus() {
                let that = this;
                that.$request({
                    url: that.$api.region.status,
                }).then(response=>{
                    that.$hideLoading();
                    if(response.code == 0) {
                        that.region = response.data.region;
                    }else {
                        uni.showToast({
                            title: response.msg,
                            icon: 'none',
                            duration: 1000
                        });
                    }
                }).catch(response => {
                    that.$hideLoading();
                });
            },

            setting() {
                let that = this;
                that.$request({
                    url: that.$api.region.setting,
                }).then(response=>{
                    that.$hideLoading();
                    that.getStatus();
                    if(response.code == 0) {
                        that.config = response.data;
                        that.template_message = response.data.template_message_withdraw;
                        for (let i = 0; i < that.config.pay_type.length; i++) {
                            switch (that.config.pay_type[i]) {
                                case "auto":
                                    that.pay_type.auto = true;
                                    //that.mark_num = 0;
                                    break;
                                case "alipay":
                                    that.pay_type.alipay = true;
                                    //that.mark_num = 2;
                                    break;
                                case "wechat":
                                    that.pay_type.wechat = true;
                                    //that.mark_num = 1;
                                    break;
                                case "balance":
                                    that.pay_type.balance = true;
                                    //that.mark_num = 4;
                                    break;
                                case "bank":
                                    that.pay_type.bank = true;
                                    //that.mark_num = 3;
                                    break;
                            }
                        }
                    }else {
                        uni.showToast({
                            title: response.msg,
                            icon: 'none',
                            duration: 1000
                        });
                    }
                }).catch(response => {
                    that.$hideLoading();
                });
            },
        },

        onLoad(options) { this.$commonLoad.onload(options);
            let that = this;
            if (options.money > 0) {
                that.money = options.money;
            };
            that.setting();
        }
    }
</script>

<style scoped lang="scss">
    .cash-type {
        height: #{120rpx};
        background: white;
        color: #353535;
        font-size: #{32rpx};
        padding: 0 #{24rpx};
        margin-top: #{24rpx};

        .arrow-image {
            width: #{12rpx};
            height: #{24rpx};
            display: block;
            margin-left: #{12rpx};
        }
    }

    .cash-info {
        margin-top: #{20rpx};
    }

    .info {
        background-color: #fff;
        padding: #{32rpx} #{24rpx};
        border-bottom: #{1rpx} solid #e2e2e2;
        color: #353535;
    }

    .info.input-money {
        border-bottom: 0;
    }

    .info.cash-type {
        margin: #{20rpx} 0;
        padding: #{32rpx} 0;
    }

    .last {
        font-size: #{40rpx};
        margin-bottom: #{15rpx};
    }

    .last-info {
        font-size: #{24rpx};
        color: #666;
    }

    .last-info view {
        float: left;
        height: #{44rpx};
        line-height: #{44rpx};
    }

    .last-info .show-tip {
        color: #666;
        display: inline-block;
        background-color: #fff;
        font-size: #{24rpx};
        padding: 0 #{12rpx};
        height: #{44rpx};
        line-height: #{44rpx};
        border-radius: #{22rpx};
        border: #{1rpx} solid #e2e2e2;
    }

    .last-info button::after {
        border: 0;
    }

    .price {
        height: #{80rpx};
        line-height: #{80rpx};
        font-size: #{46rpx};
        position: absolute;
        left: 0;
        top: 0;
        color: #ff4544;
    }

    .all {
        color: #666;
    }

    .input {
        font-size: #{36rpx};
        height: #{80rpx};
        line-height: #{80rpx};
        width: 70%;
        padding-left: #{50rpx};
        position: relative;
    }

    .input input {
        height: #{80rpx};
        line-height: #{80rpx};
    }

    .about {
        font-size: #{26rpx};
        padding: #{32rpx} #{24rpx} #{12rpx};
        color: #999;
    }

    .icon {
        height: #{40rpx};
        width: #{40rpx};
        margin-right: #{16rpx};
        display: flex;
        justify-content: center;
    }

    .mode-item {
        width: #{200rpx};
        border: #{1rpx} solid #999;
        text-align: center;
        padding: 0 #{24rpx};
        height: #{66rpx};
        line-height: #{66rpx};
        border-radius: #{33rpx};
        display: flex;
        align-items: center;
        margin: #{10rpx} #{24rpx};
        font-size: #{30rpx};
        position: relative;
    }

    .mode-title {
        margin-bottom: #{25rpx};
        padding-left: #{24rpx};
        font-size: #{34rpx};
    }

    .enter {
        color: #353535;
        float: left;
        width: #{100rpx};
        height: #{56rpx};
        line-height: #{56rpx};
        font-size: #{32rpx};
    }

    .content {
        height: #{120rpx};
    }

    .info-input {
        font-size: #{32rpx};
        height: #{56rpx};
        padding: 0 #{32rpx};
    }


    .on-active {
        height: #{32rpx};
        width: #{32rpx};
        position: absolute;
        right: 0;
        bottom: 0;
    }

    .submit {
        margin-top: #{40rpx};
    }

    .submit button {
        height: #{80rpx};
        border-radius: #{40rpx};
        line-height: #{80rpx};
        width: 90%;
        margin: 0 auto;
        color: #fff;
        font-size: #{32rpx};
        background-color: #ff4544;
    }

    .bg {
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
        background-color: rgba(0, 0, 0, .3);
        z-index: 10;
    }

    .dialog {
        position: fixed;
        top: #{400rpx};
        left: 0;
        right: 0;
        height: #{300rpx};
        width: #{640rpx};
        margin: 0 auto;
        z-index: 21;
        background-color: #fff;
        border-radius: 10px;
        text-align: center;
        font-size: #{30rpx};
    }

    .dialog-title {
        margin: #{40rpx} auto #{35rpx};
    }

    .dialog-btn {
        height: #{88rpx};
        width: #{640rpx};
        border-top: #{1rpx} solid #e2e2e2;
        line-height: #{88rpx};
        position: absolute;
        bottom: 0;
        left: 0;
    }

    .dialog-close,.dialog-submit {
        width: 50%;
    }

    .line {
        height: #{44rpx};
        margin-top: #{22rpx};
        width: #{1rpx};
        background-color: #e2e2e2;
    }

    .dialog-submit {
        color: #ff4544;
    }

    .bg {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: 100%;
        width: 100%;
        z-index: 20;
        background-color: rgba(0, 0, 0, 0.3);
    }

    .dialog {
        position: fixed;
        top: #{400rpx};
        left: 0;
        right: 0;
        height: #{300rpx};
        width: #{640rpx};
        margin: 0 auto;
        z-index: 21;
        background-color: #fff;
        border-radius: 10px;
        text-align: center;
        font-size: #{30rpx};
    }

    .close-button {
        height: #{88rpx};
        width: #{640rpx};
        border-top: #{1rpx} solid #e2e2e2;
        line-height: #{88rpx};
        color: #ff4544;
        position: absolute;
        bottom: 0;
        left: 0;
    }

    .dialog-title {
        margin: #{40rpx} auto #{35rpx};
    }
</style>